using System.Windows;

namespace LicenseApp.Client.Examples;

/// <summary>
/// 对话框测试窗口
/// </summary>
public partial class DialogTestWindow : Window
{
    public DialogTestWindow()
    {
        InitializeComponent();
    }

    private void BtnTestRegistrationDialog_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            txtStatus.Text = "正在显示授权注册对话框...";
            
            var dialog = new LicenseRegistrationDialog(
                "这是测试消息：需要注册授权码才能使用系统功能。\n\n" +
                "对话框包含公钥检查和导入功能。", 
                this
            );
            
            dialog.ShowDialog();
            
            if (dialog.IsRegistered)
            {
                txtStatus.Text = "✅ 授权注册成功！";
            }
            else
            {
                txtStatus.Text = "❌ 用户取消了授权注册";
            }
        }
        catch (Exception ex)
        {
            txtStatus.Text = $"❌ 测试失败: {ex.Message}";
        }
    }

    private void BtnTestNonBlockingDialog_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            txtStatus.Text = "正在显示非阻塞对话框...";
            
            LicenseHelper.ShowNonBlockingDialog(
                "测试标题",
                "这是一个非阻塞对话框测试。\n\n" +
                "它会在5秒后自动关闭，不会阻塞主程序运行。",
                LicenseDialogType.Information,
                this
            );
            
            txtStatus.Text = "✅ 非阻塞对话框已显示（5秒后自动关闭）";
        }
        catch (Exception ex)
        {
            txtStatus.Text = $"❌ 测试失败: {ex.Message}";
        }
    }

    private void BtnTestLicenseHelper_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            txtStatus.Text = "正在执行一键集成测试...";
            
            var result = LicenseHelper.InitializeLicense(
                parentWindow: this,
                autoStartMonitoring: false,  // 测试时不启动监控
                onStatusChanged: OnTestStatusChanged
            );
            
            if (result.Success)
            {
                txtStatus.Text = "✅ 一键集成成功！授权系统已初始化";
            }
            else
            {
                txtStatus.Text = $"⚠️ 一键集成结果: {result.Message}";
                
                if (result.NeedsLicenseRegistration)
                {
                    txtStatus.Text += "\n📝 已自动显示授权注册对话框";
                }
            }
        }
        catch (Exception ex)
        {
            txtStatus.Text = $"❌ 测试失败: {ex.Message}";
        }
    }

    private void OnTestStatusChanged(LicenseStatusInfo statusInfo)
    {
        Dispatcher.BeginInvoke(() =>
        {
            txtStatus.Text += $"\n📊 状态变化: {statusInfo.Status} - {statusInfo.Message}";
        });
    }
}
